#!/bin/bash
#检查是否在Git仓库中
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    echo "错误: 当前目录不是Git仓库"
    exit 1
fi
echo "注意！提交前必须保证仓库处于同步状态,否则脚本无法正常执行!"
printf "本地远端仓库同步中...是否同步? (y/n) : "
read -r pull_cmd
# echo "$pull_cmd"
YES="y"
NO="n"
if [ "$pull_cmd" = "$YES" ]; 
then 
    echo "git pull"
else
    printf "已退出done\n"
    exit 0
fi
add_files=()  # 初始化空数组
index=1       

echo "执行add,请输入要提交的文件名,按回车键分割,不输入直接回车将会结束输入操作:"
while read -p "请输入: " -r add_file; do
    # 正确的空值检查
    if [ -z "$add_file" ]; then
        echo "获取add文件done!"
        break
    fi
    # 将用户输入添加到数组末尾
    add_files+=("$add_file")
    # 显示当前数组内容
    echo "已添加: $add_file : done"
done
echo "执行add命令..."
echo "test add : git add ${result}"
echo "add done!"
echo "执行commit,请输入备注,按回车键分割,不输入直接回车将会结束输入操作: "
commit_texts=()
while read -p "请输入: " -r commit_text; do
    # 正确的空值检查
    if [ -z "$commit_text" ]; then
        echo "退出"
        break
    fi
    # 将用户输入添加到数组末尾
    commit_texts+=("$commit_text")
    # 显示当前数组内容
    echo "已添加: $commit_text : done"
done


echo "开始推送至远程仓库,请等待..."
for ((i=0; i<${#add_files[@]}; i++)); do
    add_file="${add_files[$i]}"
    message="${commit_texts[$i]}"
    git add $add_file
    git commit -m "$message"
    if [ $? -ne 0 ]; then
        echo "警告: 提交失败（可能没有文件变更）"
    fi
done

echo "开始push,请输入gitee提交的账号和密码:(tips: 如果是windows系统会直接跳过,前提是你看过鹏哥的视频...) "
printf "账号： "
read -p "请输入: " -r username;
printf "密码: "
read -p "请输入: " -r pwd;


# 临时设置凭证缓存
git config --local credential.helper store

# 写入凭证到缓存
echo "https://${username}:${pwd}@gitee.com" > ~/.git-credentials
echo "开始push...." 
git push 

